home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 24 (1991-10)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].zip / MegaDisc 24 (1991-10)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].adf / TUTES_&_CLI / Basic_Tutorial_3&5 < prev    next >
Text File  |  1991-09-26  |  29KB  |  827 lines

  1.  
  2.  
  3.                            BASIC TUTORIAL   
  4.  
  5.                             PARTS 3 & 5
  6.  
  7.     As there was an unfortunate omission on MEGADISC23 I have decided to
  8.   include the text for Chapter Three along with Chapter Five.
  9.  
  10.     Also in this drawer is "EducatingSally" - this is a program which is
  11.   used in conjunction with Tutorial #5, and includes all references from
  12.   part #3 of the Tutorial - note that the less finished version of
  13.   "EducatingSally" which Part #3 refers to was in MD23. [Ed: Mea culpa!]
  14.  
  15.  
  16.  
  17.     ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ##
  18.  
  19.  
  20.  
  21.                           BASIC TUTORIAL    
  22.  
  23.                             PART THREE
  24.  
  25.                                by
  26.  
  27.                          Frank Wilkinson
  28.  
  29.   In this chapter we are going to look at the outline of the Program
  30.   we are going to construct.  As well as learning to apply the commands
  31.   we have already learnt we will also learn some new ones.
  32.  
  33.   The first thing any programmer must do is sit down and work out just
  34.   what it is that he want his program to do.
  35.  
  36.   So!
  37.  
  38.   The very first thing we will want is a SETTING UP SECTION, usually
  39.   called the INITIALIZATION. Here we have to decide how big our
  40.   screen will be and what our COLOR scheme will be and allow for the
  41.   colours we want to use.
  42.  
  43.   Then we will need to INITIALIZE any windows that we are going to use.
  44.  
  45.   Here also we can initialize any arrays we may want to use.
  46.  
  47.   After that we will need to set up a TITLE SCREEN. We learnt how to
  48.   make a title screen with our Colors program last chapter (#2). This will
  49.   do very nicely with a few modifications.
  50.  
  51.   As our program is going to be a multi-purpose Educational program we
  52.   could possibly have a second screen briefly describing its purpose.
  53.  
  54.   Then we will need some means of making a choice. Do we want to learn
  55.   addition or multiplication or spelling or maybe some geography?
  56.  
  57.   ( This program will be very flexible. Once you have learnt how to
  58.   construct the program. You will be able to patch in your own MODULES
  59.   so that you can tailor the program to teach anything from ASTRONOMY
  60.   to ZOOLOGY.
  61.  
  62.   When we have made our choice then we have to have the means to get
  63.   where we want to go.
  64.  
  65.     ABOUT LABELS
  66.  
  67.   I have used 'LABELS', But I haven't explained them yet. So before we
  68.   go any further let's take a look at 'LABELS'.
  69.  
  70.   AMIGABasic doesn't have line numbers like most other BASICS instead
  71.   it uses 'LABELS'.
  72.  
  73.   A LABEL can be any string you like! Letters or letters and numbers
  74.   or if you like just numbers. These are some examples.
  75.  
  76.   Begin:
  77.   Init:
  78.   Addition:
  79.   Addition2:
  80.   100 :
  81.   RestAWhile:
  82.   ReadAFile:
  83.   WriteAFile:
  84.   10000000000 :
  85.  
  86.   They should be descriptive and they can even contain BASIC KEYWORDS.
  87.   If a LABEL is a number you must always put a space after the number
  88.   before the colon. All LABELS are recognised by AMIGABasic when they
  89.   end with the colon.
  90.   AMIGABasic stores the names of all the LABELS and where they are. So
  91.   that it knows where to go when you 'GOTO LABELNAME'.
  92.  
  93.   If our labelname is a subroutine it must always end with a RETURN
  94.   STATEMENT. (More about SUBROUTINES later.)
  95.  
  96.   We use LABELS to write our routines, such as Spelling, Addition. We
  97.   will learn how to make these routines as we build up the program.
  98.  
  99.   We will also make use of SUBROUTINES, The beauty of a SUBROUTINE is
  100.   that it can be called from any part of our program. We can then use
  101.   the SUBROUTINE and when we have finished with it we can RETURN to the
  102.   place we came from.
  103.   This enables us to save DISK SPACE, REDUCES THE SIZE OF OUR PROGRAM,
  104.   but best of all we only have to write the routine once.
  105.  
  106.   I have started a LIBRARY OF SUB-ROUTINES for you.
  107.  
  108.   In our CHOICE MENU we should always have the ability to end the
  109.   program cleanly so our last Item will always be "Finished with this
  110.   Program".
  111.  
  112.   We can think about other CHOICE menus which we can use in our Modules.
  113.   We may want to have the choice of:
  114.  
  115.             1.  Multiply two single digit numbers
  116.             2.  Multiply two double digit numbers.
  117.             3.  Multiply Double digit by Triple digit number.
  118.  
  119.   So you see you can have a CHOICE within a CHOICE.
  120.  
  121.  
  122.  
  123.   INITIALIZATION  
  124.  
  125.   Now that we know how our Program is going to be organized we can set
  126.   about our First task - 'INITIALIZATION'.
  127.  
  128.   In the INITIALIZATION you can put a few REM STATEMENTS. These can
  129.   describe the Program and give details of who wrote it.
  130.  
  131.   Feel free to put your own name in here because when you adapt it for
  132.   your own needs. You will have written it yourself.
  133.  
  134.   It is very hard to define Copyright. Here we are using Microsoft
  135.   BASIC for the Amiga. Microsoft own the copyright to AMIGABasic.
  136.   I have to use Microsoft's KEYWORDS and PARAMETERS or the program
  137.   will not work. So in a way every program in Microsoft Basic is
  138.   copyrighted by Microsoft. So whatever the pros and cons are of
  139.   copyright you can rest assured that this TUTORIAL is for you to USE.
  140.   I want you to use it. That's what it is all about.
  141.  
  142.  
  143.   A FEW COMMANDS  
  144.  
  145.   Before we go to the Program there are some commands we have to learn.
  146.  
  147.   1......GOTO
  148.   2......GOSUB
  149.   3......IF.....THEN
  150.   4......IF.....THEN.......ELSE
  151.   5......RANDOM numbers more advanced
  152.   6......LINE
  153.   7......RIGHT$,LEFT$ and MID$.
  154.  
  155.   Not many but very important.
  156.  
  157.   1.
  158.   The GOTO command tells Basic the next thing we want to do is elsewhere
  159.   in the program and we want to go there.
  160.  
  161.   Now if you remember when we were talking about labels I said a label
  162.   was used with the GOTO and GOSUB commands like this:-
  163.  
  164.               GOTO Labelname.
  165.  
  166.   The program immediately goes to the label Labelname and continues from
  167.   there. In other words we can skip about in our program by using the
  168.   GOTO command.
  169.   This can be very DANGEROUS if you aren't careful. With too many GOTO's
  170.   following each other you can soon get lost. GOSUBs are a better way
  171.   to move about your program because when you are finished they always
  172.   RETURN you to the line after you called the GOSUB.
  173.   It is not always practical to use a GOSUB command. In these cases you
  174.   will just have to use a GOTO. In our program you will see several
  175.   GOTOs and they are essential in this context. When we are finished
  176.   with the part of the program we have gone to. I have always sent the
  177.   program back to the choice screen.
  178.  
  179.   2.
  180.   GOSUB.
  181.  
  182.   A GOSUB SUBROUTINE is different from a SUB...END SUB.
  183.   A SUB....END SUB is called and used just like a command. But a GOSUB
  184.   is a whole new ball game. It can be very long and it can call other
  185.   GOSUBs and they will always return you to the Statement after you
  186.   called them. They can be used any where in your program, but if we are
  187.   to make sense out of our program and make it possible for others to
  188.   read we will keep them altogether in a little section of their own just
  189.   before the SUB...END SUBS at the end of the program.
  190.  
  191.   The GOSUB uses the same kind of syntax as the GOTO like this:-
  192.  
  193.              GOSUB Labelname
  194.  
  195.   The GOSUB then goes to the LABEL Labelname, does whatever it is
  196.   designed for and when it reaches the RETURN command at the end of the
  197.   SUBROUTINE it goes back to the statement after the line it was called
  198.   from.
  199.   In our program you will see these GOSUBs and they look like this:-
  200.  
  201.   IF answer1=number1+number2 THEN GOSUB Correct :GOTO RestAWhile1
  202.   IF answer1<>number1+number2 THEN GOSUB Wrong :GOTO Try1
  203.  
  204.   Here we have two IF...THEN statements which check to see whether the
  205.   answer to a question is true or false.
  206.  
  207.   If it is true then the GOSUB sends the program to the SUBROUTINE
  208.   'Correct'. It performs the tasks in the subroutine and when it
  209.   reaches the RETURN statement at the end of the subroutine the program
  210.   RETURNs to the next statement which says GOTO RestAWhile. Which is an
  211.   INKEY$ statement which makes the computer wait for a key to be pressed.
  212.  
  213.   If it is false (i.e. we got the answer wrong) then the program goes
  214.   to the SUBROUTINE 'Wrong' does what it has to, then RETURNs to the
  215.   GOTO Try1 label which allows us to have another try at getting the
  216.   answer right.
  217.  
  218.   By putting multiple statements on one line, if the IF statement fails
  219.   it will drop through to the next line and completely ignore anything
  220.   on the line after the failed statement.
  221.  
  222.   It will only read the statements after the THEN if the first statement
  223.   is true.
  224.  
  225.   3.
  226.   IF......THEN
  227.  
  228.   This is the simplest form of the IF....THEN statement and it only
  229.   does one thing.
  230.             IF (this statement is true) THEN (do this)
  231.  
  232.             IF (this statement is not true) THEN (do nothing and go to
  233.             the next line)
  234.  
  235.   In our choice section you will see several IF......THENs like this:-
  236.  
  237.             IF n<1 or n>7 THEN GOTO Choice
  238.             IF n=1 THEN GOTO Addition
  239.             ...........
  240.             ...........
  241.             IF n=7 THEN GOTO Cleanup
  242.             GOTO Choice
  243.  
  244.   What this does is make some decisions for us.
  245.  
  246.   The first line says IF the number we type in is less than "1" or
  247.   greater than "7" then go back to Choice and have another try.
  248.  
  249.   The second line says IF the number we have typed in is equal to "1"
  250.   THEN off you GOTO Addition and do whatever has to be done.
  251.  
  252.   The last line says IF the number we have typed in is equal to "7"
  253.   THEN we have finished with this program. GOTO Cleanup and close down.
  254.  
  255.   4.
  256.   IF......THEN......ELSE
  257.  
  258.   This is a little bit more advanced than the 'IF.....THEN' statement.
  259.   It can decide between 2 alternative statements.
  260.  
  261.   IF statement one is true THEN do statement two.   ELSE
  262.   IF statement one is false do statement three.
  263.  
  264.   It sounds a little complicated but it works like this.
  265.  
  266.   In our imaginary program we could have a variable which is set by a
  267.   RANDOM NUMBER GENERATOR and IF the random number is less than 5 say.
  268.   We want the program to print "HEADS". And IF it isn't less than
  269.   5 THEN we  want to print "TAILS". Here's how we would do it:-
  270.  
  271.   A=INT(RND*10)
  272.   IF A<5 THEN PRINT "HEADS" ELSE PRINT "TAILS"
  273.  
  274.   5.
  275.   RANDOM numbers again.
  276.  
  277.   Whilst we are talking about RANDOM NUMBERS. I have used a slight
  278.   variation of the RND command in our program this time and you will
  279.   find it very useful if you need specific numbers between a range of
  280.   numbers.
  281.  
  282.   Say you want to generate numbers between 50 and 100 and nothing else.
  283.   You would use a command like this:
  284.  
  285.   Number=Starting number + ((Ending number-Starting number)*RND)
  286.  
  287.   Which in our case would be:-
  288.  
  289.   Number=50 + ((100-50)*RND)
  290.  
  291.   Using this form of the RND command we will only get random numbers
  292.   between 50 and 100.
  293.  
  294.   I have used this method in the arithmetic section of our program.
  295.  
  296.  6....LINE
  297.  
  298.   There are several forms of the LINE command and the simplest one will
  299.   just draw a LINE:-
  300.  
  301.            START            TO       END             COLOR
  302.   LINE (xposition,yposition) - (xposition,yposition),red
  303.  
  304.   Try to imagine your screen as a sheet of graph paper. Like you used
  305.   at school.
  306.  
  307.                  'x'  position
  308.     0         5         10        15        20        25        30
  309.    0-------------------------------------------------------------------
  310.     |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  311. Y   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  312.     |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  313. p   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  314. o  5|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  315. s   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  316. i   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  317. t   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  318. i   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  319. o 10|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  320. n   |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  321.     |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  322.     |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
  323.     -------------------------------------------------------------------
  324.  
  325.  
  326.   The numbers across the top are the 'x' co-ordinates (position) of the
  327.   Pixel you want to start or finish at.
  328.   The numbers down the side are the 'y' co-ordinates (position) of the
  329.   Pixel you want to start or finish at.
  330.  
  331.   A PIXEL is a Picture Element and in MED-Res you have 640 Pixels across
  332.   the screen and 256 down the screen.
  333.  
  334.   If you are drawing in a WINDOW remember the borders, they reduce the
  335.   number of Pixels you have to draw on. To about 631 across and 241 down,
  336.   depending of course on the size of your window.
  337.  
  338.   So if your LINE statement says:-
  339.  
  340.   LINE (5,5)-(40,5),red
  341.  
  342.   It means start at the '5th' Pixel across and the '5th' Pixel down.
  343.   Draw a line to the '40th' Pixel across and the '5th' Pixel down.
  344.   And I want the color of the line to be red.
  345.  
  346.   Remember you can only use the color red if you have set up your
  347.   palette like we did in our colors program and then set our variable
  348.   to red. Otherwise you will have to remember which palette number is
  349.   red and use that number.
  350.  
  351.   The next variation of the LINE command draws an empty Box or Rectangle.
  352.  
  353.   LINE (5,5)-(40,40),red,b
  354.  
  355.   In this case the (5,5) is the TOP LEFTHAND corner of the BOX.
  356.   and (40,40) is the BOTTOM RIGHTHAND corner of the BOX and this will
  357.   give you something like this:-
  358.  
  359.     (5,5)
  360.   ....|----------------------------------|..........
  361.   ....|                                  |..........
  362.   ....|                                  |..........
  363.   ....|                                  |..........
  364.   ....|                                  |..........
  365.   ....|                                  |..........
  366.   ....|                                  |..........
  367.   ....|                                  |..........
  368.   ....|                                  |..........
  369.   ....|                                  |..........
  370.   ....|                                  |..........
  371.   ....|                                  |..........
  372.   ....|----------------------------------|..........
  373.                                       (40,40)
  374.  
  375.   THE empty BOX will be drawn in red.
  376.  
  377.   Another variation is by using 'bf' instead of 'b' like this:-
  378.  
  379.   LINE (5,5)-(40,40),red,bf
  380.  
  381.   This statement will do exactly the same as the previous one with
  382.   one major difference.
  383.   Instead of an empty BOX you will get a beautiful all in living color
  384.   red Box. i.e it fills the empty BOX with the red color.
  385.   Hence 'bf' = BOXFILL
  386.  
  387.   There is another LINE command and that is:-
  388.  
  389.   LINE (5,5)-(40,5)   Draws a straight line
  390.   Line -(40,40)       This one starts where the first one finished
  391.                       and Draws a straight line to '40,40 like this:-
  392.  
  393.  5,5                                        40,5
  394.    -------------------------------------------|
  395.                                               |
  396.                                               |
  397.                                               |
  398.                                               |
  399.                                               |
  400.                                               |
  401.                                               |
  402.                                               |
  403.                                               |
  404.                                             40,40
  405.  
  406.   We use the 'bf' LINE command in our "Educating Sally" program. When
  407.   you have run the program, look at the listing. You will find it in
  408.   every section of the program that I have written so far. In the next
  409.   chapter. You will find that I have converted it to a SUBROUTINE along
  410.   with several others. Hopefully this will give you a practical demo of
  411.   how and why we use SUBROUTINES.
  412.  
  413.   7.....RIGHT$
  414.  
  415.   This is one of a group of THREE commands which handle STRINGS.
  416.   i.e "This is a STRING"
  417.   Any group of CHARACTERS enclosed in " " (Double Quotes) is a STRING.
  418.  
  419.   Nam$="Educating Sally"
  420.  
  421.   If we say in our Program 'PRINT Nam$'
  422.   the computer will PRINT
  423.  
  424.   Educating Sally.
  425.  
  426.   "Educating Sally" has 15 characters. Go on count them.
  427.  
  428.   Don't forget the space between "Educating" and "Sally" is also a
  429.   character just as are 8,7,*,_,&,>,<.
  430.  
  431.   PRINT RIGHT$(Nam$,5)
  432.  
  433.   The Command RIGHT$ tells the computer to PRINT the last 5 characters
  434.   of the string Nam$. The last 5 characters are S,a,l,l,y so the
  435.   computer will PRINT
  436.  
  437.   Sally
  438.  
  439.   Conversely (I looked it up in the dictionary) the command LEFT$ tells
  440.   the computer to start at the left or start of the string.
  441.   So if we say:-
  442.  
  443.   PRINT LEFT$(Nam$,9)
  444.  
  445.   the computer will PRINT:-
  446.  
  447.   Educating.
  448.  
  449.   Now comes the tricky bit. I always have to think about this one.
  450.  
  451.   MID$
  452.  
  453.   This tells the computer we want something from the MIDdle of the
  454.   string. But where in the middle. Here we have an extra PARAMETER.
  455.  
  456.   PRINT MID$(Nam$,4,3)
  457.                   |
  458.          " extra parameter"
  459.  
  460.   This says get the string called Nam$ which we know is "Educating Sally"
  461.   start at the 4th characters and PRINT 3 characters.   (   456   )
  462.  
  463.   This gives us:-
  464.  
  465.   cat.
  466.  
  467.   To see if you have got this by the short and curlies, see if you can
  468.   unravel this one without the aid of the computer:
  469.  
  470.   Ani$="liontigerstorkorkygorillamarmadillo"
  471.   PRINT MID$(ani$,10,5);" ";MID$(ani$,19,7);" ";RIGHT$(ani$,9);" ";
  472.   PRINT MID$(ani$,10,5);" ";MID$(ani$,23,5);" and ";MID$(ani$,14,5);
  473.   PRINT " the ";MID$(Nam$,4,3).
  474.  
  475.  
  476.   Now on with the Program.
  477.  
  478.  
  479.   In our INITIALIZATION we will use the Colors program and so the choice
  480.   of Screen,Window and colours will be as is.
  481.   That is a Medium Resolution Screen with 8 Colours and we will use
  482.   the biggest Window we can for our TITLE screen.
  483.   Later we may wish to change the size and colours of our window. When
  484.   we do I will explain what I have done and how you can customize your
  485.   own screens and windows.
  486.  
  487.   After we have set up our Screen, Window and Colours we can the go about
  488.   designing our TITLE SCREEN.
  489.  
  490.  
  491.   First lets give it a label. The obvious one is:-
  492.  
  493.   TitleScreen:
  494.  
  495.   I will not write it twice just load the program 'EDUCATING SALLY'
  496.   and read the REM STATEMENTS.
  497.  
  498.   I have Initialized the SCREEN and first window.
  499.  
  500.   I have also created WINDOW 3 in each section. If you look carefully
  501.   you will see that it is the same window in each section but with a
  502.   different name.
  503.  
  504.   We can do this as long as we remember to close this window when we
  505.   are finished using it.
  506.  
  507.   And we do that with this command:-
  508.  
  509.   WINDOW CLOSE 3.
  510.  
  511.   I have implemented all the Addition choices. Which are:-
  512.   1.....Add Single digit numbers.
  513.   2.....Add Double digit numbers.
  514.   3.....Add Three digit numbers.
  515.   4.....Add mixed digit numbers.
  516.  
  517.   All the other choices will say:-
  518.  
  519.   Not Implemented yet
  520.   Press any key to continue.
  521.  
  522.   Study the listing carefully and you will see that to implement the
  523.   Subtraction, and Multiplication is only a matter of a few minor
  524.   alterations and if you wish make a copy of the listing and see if
  525.   you can do it yourself.
  526.  
  527.   The Division will be a little harder as you have to take care of the
  528.   bits that are left over (i.e. The remainder ) in a Division like this
  529.  
  530.   333 / 10
  531.  
  532.   answer=33.33333333333333333333333333333 for ever.
  533.  
  534.   or
  535.  
  536.   33 with 3 over.
  537.  
  538.   Think about it for a while because next time we are going to start
  539.   on the Spelling and Geography. I will leave the other Arithmetic till
  540.   last.
  541.  
  542.   But to give you some idea of what to do...
  543.  
  544.   Change the '+' in the PRINT USING "####";number1;:PRINT "+"
  545.   to a '-' or a '*' sign.
  546.   Then change the LOCATE 8,10:PRINT "Add up the lefthand column "
  547.   to
  548.   LOCATE 8,10:PRINT "Subtract the lefthand column "
  549.   etc., etc.
  550.  
  551.   You won't want these lines
  552.   LOCATE 8,10:PRINT "Type in the carry number    "
  553.   INPUT "        ";answer3
  554.   LOCATE 6,10:IF answer3=0 THEN PRINT " " ELSE PRINT STR$(answer3)
  555.   LOCATE 8,1:PRINT "
  556.  
  557.   Multiply is a bit harder but it should test your new skills.
  558.  
  559.  
  560.   ************************************************************************
  561.  
  562.  
  563.  
  564.                         Basic Tutorial   
  565.  
  566.                         Chapter Five 
  567.  
  568.                              by
  569.  
  570.                        Frank Wilkinson
  571.  
  572.       In this chapter we are going to put the finishing touches to our
  573.   first real program. There is not a lot that is new in this part. The
  574.   only command that is completely new is the VAL() command.
  575.   This command is used to convert a STRING into numerical value, as we
  576.   discovered in an earlier chapter. If we convert a number to a STRING
  577.   we can more easily manipulate the individual parts of the number.
  578.  
  579.   If we have a numeric variable 'A' containing the number 586441 we can
  580.   convert the number to a STRING variable with the comand:-
  581.  
  582.   A$=STR$(A)
  583.  
  584.   and now we have a STRING of ASCII Characters and not a number.
  585.   You will remember from previous chapters that the commands to extract
  586.   individual parts of the STRING are:-
  587.  
  588.   LEFT$(A$,?)
  589.                           MID$(A$,?,?)
  590.                                                   RIGHT$(A$,?)
  591.  
  592.   Where the '?' represents a number or a position in the STRING.
  593.   If you are not sure about the use of these command go back to chapter
  594.   Three and read it again. At the same time you can find the commands in
  595.   your Basic Manual and study them there.
  596.  
  597.   As I pointed out earlier I am glad the tax man doesn't use STRINGS to
  598.   calculate how much tax we have to pay, because when we add STRINGS we
  599.   just join the STRINGS together.
  600.  
  601.  
  602.   a=123
  603.   b=456
  604.   a$=STR$(a)
  605.   a$=" 123"
  606.   b$=STR$(b)
  607.   b$=" 456"
  608.  
  609.   If we add these STRINGS we get:-
  610.  
  611.   X$+Y$= 123 456
  612.  
  613.        ****************************************************************
  614.  
  615. 'Example 1
  616.     a=123
  617.     b=456
  618.     a$=STR$(a)
  619.     b$=STR$(b)
  620.     x$=a$+b$
  621.     PRINT "a="a,"a$="a$,"Length a$="LEN(a$)
  622.     PRINT "b="b,"b$="b$,"Length b$="LEN(b$)
  623.     PRINT "x$="x$,"Length x$="LEN(x$)
  624.     PRINT "val(x$)="VAL(x$),"length VAL(x$)="LEN(x$)-1
  625.  
  626.     ****************************************************************
  627.  
  628.   If you cut out Example 1 in a text editor or if you can't manage
  629.   that yet just carefully type it in to basic and you will get an output
  630.   like this:-
  631.  
  632.   a= 123       a$= 123           Length a$= 4
  633.   b= 456       b$= 456           Length b$= 4
  634.   x$= 123 456                    Length x$= 8
  635.   val(x$)= 123456                Length VAL(x$)= 7
  636.  
  637.   Now before we have anyone ringing up from Germany or elsewhere in
  638.   the middle of the night and saying:
  639.  
  640.   "In Chapter 5 you have made an error. There is no such command as
  641.   LEN(VAL(x$))."
  642.   I have cheated just a little, to try and make something clearer.
  643.  
  644.   If you look at the output CAREFULLY you will see:-
  645.  
  646.   a= 123
  647.  
  648.   Not what I told Basic, which was "a=123"
  649.   What does that extra space mean?
  650.   Well Integer Numbers (there are many more types of numbers: floating
  651.   point, real, complex, imaginary) can be regarded for our purpose to be
  652.   of 2 types.
  653.  
  654.   1. Positive
  655.   2. Negative
  656.  
  657.   In BASIC the "+" sign is not displayed but the number has a space
  658.   in front of it.
  659.   On the other hand a negative number is displayed with a "-" in front
  660.   of it like so:-
  661.   a= 123
  662.   b=-123
  663.  
  664.   So each number is actually 1 character longer than the number of
  665.   digits it contains. When these numbers are converted to STRINGS then
  666.   the space or "-" sign are included. So that when we add the two small
  667.   STRINGS " 123" AND " 456" We get a larger string containing all 8
  668.   characters " 123 456" and our 2 numbers of 3 digits each become one
  669.   large string of 8 characters. This part also applies to negative
  670.   numbers. Adding 2 negative STRINGS "-123" + "-456" will give "-123-456"
  671.   But please be very careful as the VAL() command is not able to recognise
  672.   "-" signs or for that matter "+" signs. What it can do, and indeed does
  673.   do, is to ignore SPACES. Unless the "+" or "-" is the first character
  674.   of a STRING.
  675.  
  676.   So how do we convert the STRINGS back to NUMBERS?
  677.  
  678.   We use the command:-
  679.  
  680.   Number = VAL(a$)      or    Number2 = VAL(b$)
  681.   or in our case VAL(x$)
  682.  
  683.   Now the VAL() command will convert any STRING containing digits into
  684.   a number. But, and I emphasize this, as soon as it meets a character
  685.   which is not a digit it STOPS. The only character it will step over
  686.   is a SPACE. There is one exception, if the FIRST character is a "+"
  687.   or a "-" sign, VAL() will recognise it.
  688.  
  689.   STRING             RESULT
  690.   " 123 456"          123456
  691.   " 123-456"          123
  692.   "-123-456"         -123
  693.   " 123abcd"          123
  694.   " 456 ab3"          456
  695.   "-abc2345"          0
  696.   "-0345678"         -345
  697.   "-034.567"         -34.567
  698.  
  699.   Zero " 0" is always regarded as a positive number unless it is
  700.   followed by another number.
  701.   The "." decimal point is regarded as a digit by VAL().
  702.  
  703.  
  704.  
  705.   RANDOM NUMBER GENERATORS
  706.  
  707.   As a final point on the generation of RANDOM NUMBERS I have included
  708.   here 2 RANDOM NUMBER GENERATORS.
  709.   They will both generate RANDOM NUMBERS between 1 and any number you
  710.   select. You can cut them out with a text editor or type them in. They
  711.   will both display the highest number generated. If you have a couple
  712.   of days to spare you can make the loop = to 5000000 but I don't
  713.   recommend it. It wont make any difference.
  714.  
  715.  
  716.   *********************************************************************
  717.  
  718.  
  719. Example 2
  720.  
  721. RANDOMIZE TIMER
  722.  
  723. x=0
  724. FOR num =1 TO 10000
  725. number = RND
  726.  
  727. PRINT number;"     ",number*19;" ",number*19+.4,(number*19+.4)+1,INT(number*19+.4)+1
  728. IF number>x THEN x = number
  729.  
  730. NEXT
  731. PRINT "The highest number generated was ";x;"whose integar = ";INT(x*19+.4)+1
  732.  
  733. ***************************************************************************
  734.  
  735. INPUT "type in High number (say 60 or 40) ";y
  736. INPUT "Type in Number of times to Run (say 100 or 1000) ";z
  737.  
  738. RANDOMIZE TIMER
  739. x=0
  740. FOR num =1 TO z
  741. number = RND
  742. PRINT number;"     ",number*y;" ",INT(number*y)+1
  743. IF number>x THEN x = number
  744.  
  745. NEXT
  746. PRINT "The highest number generated was ";x;"whose integar result = ";INT(x*y)+1
  747. ***************************************************************************
  748.  
  749.   For all those people out there who like to play with random numbers
  750.   The pseudo random numbers generated by a computer can range from
  751.   .0000001 to .9999999 in THEORY. Bigger computers than the Amiga can
  752.   generate numbers with more DECIMAL places but it doesn't matter one
  753.   iota.
  754.   The number never reaches 0 and never reaches 1 so as long as your
  755.   life doesn't depend on it go for broke.
  756.  
  757.   Now that our Maths are finished I thought you might like to learn
  758.   about another kind of RANDOM. In the next chapters we are going to
  759.   deal with SEQUENTIAL and RANDOM files. How they work and how to use
  760.   them. We will design a DATABASE that will allow you to create a FILE.
  761.   Enter DATA into it, SEARCH it for an entry, ALTER an entry and Print
  762.   out your DATA. After that we will design a PAINT PROGRAM, it won't be
  763.   quite as good as DPAINT but you will be surprised at what it can do.
  764.   and if you just want to design CHRISTMAS CARDS in 32 colours this is
  765.   the one for you.
  766.  
  767.   As the next issue of Megadisc will be out before Xmas I have
  768.   decided to give you a Christmas Present. The Next Chapter is a full
  769.   fledged DataBase and is complete in every detail. It has the following
  770.   functions:-
  771.  
  772.   1.  It will create and maintain any number of files to use within the
  773.       DataBase.
  774.  
  775.   2.  Each File can have up to 10 fields in it.
  776.  
  777.   3.  As is, each file can hold 100 records with up to 10 Fields, but this
  778.       can easily be changed to 1000 or 10000, it all depends on how much
  779.       disk space you wish to allocate to each file.
  780.       If you only have 25 Names and addresses in your christmas card
  781.       list then it is pointless using a database with 10000 entries.
  782.       But if you have a stamp collection with say 4000 stamps and want
  783.       them all in one file then a file with only 100 records would be
  784.       useless.
  785.  
  786.   4.  It has a Search facility on any field with in the record.
  787.  
  788.   5.  It can print out the entire contents of your file either in list
  789.       form or as Stored.
  790.  
  791.   6.  It can print out a single entry in either List form or as stored.
  792.       Each of these methods will print a header showing the file name
  793.       and the date you printed it.
  794.  
  795.   7.  There is also a facility to print an Envelope with your return
  796.       address in the Top Left Hand corner of the Envelope and the name
  797.       and address of the addressee in the correct place.
  798.       This facility is only useful for addressbook type files but it's
  799.       there if you want it.
  800.  
  801.   8.  It comes with a full tutorial as Chapter 6 of the Basic Tutorial:
  802.       How it is constucted. How and why each command is used. And to
  803.       help you get started there are two demo files to show you how it
  804.       can be tailored to suit your needs.
  805.  
  806.   9.  It uses all the commands and subroutines we have learnt so far
  807.       plus a few more, and of course there is the usual Tutorial.
  808.  
  809.  
  810.     Till next time HAPPY COMPUTING.
  811.  
  812. Frank Wilkinson
  813. 16 Melbourne Place
  814. Alberton SA 5014.
  815.  
  816. Phone 08 47 1871
  817.  
  818.  
  819.  
  820.     ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ## 24 ##
  821.  
  822.  
  823.  
  824.  
  825.